package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.BookDao;
import com.aaa.book_mall.dao.ShopDao;
import com.aaa.book_mall.dao.impl.ShopDaoImpl;
import com.aaa.book_mall.entity.Book;
import com.aaa.book_mall.entity.Shop;
import com.aaa.book_mall.entity.Staff;
import com.aaa.book_mall.entity.User;
import com.aaa.book_mall.util.LayTab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * fileName:ShopController
 * description:
 * author:付亚星
 * createTime:2021/2/21 9:38
 * version:1.0.0
 */
@RestController
@RequestMapping("/shop")
public class ShopController extends HttpServlet {

    @Autowired
    private ShopDao shopDao;
    @Autowired
    private BookDao bookDao;


    @RequestMapping("/showAllShop")
    public ModelAndView showAllShop(HttpServletRequest request,HttpServletResponse response){
        System.out.println("进入购物车查询。。。");
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        if(user!=null){
            //        获取session中的用户编号
            Integer uId = user.getUid();
            System.out.println(uId);
            //根据uid查询购物车数据
            List<Shop> shops = shopDao.showAllShop(uId);
            System.out.println(shops);
            //先将staff存储session中
            //request.getSession().setAttribute("shop",shops);

            modelAndView.setViewName("gouwuce");//跳转页面
            modelAndView.addObject("shop",shops);//携带数据
            return modelAndView;
        }else {
            HashMap<String, Object> msp = new HashMap<>();
            msp.put("loginmsg","还没有登录，请先登录");
            session.setAttribute("loginmsg",msp);
            modelAndView.setViewName("userlogin");
            System.out.println("没有登录");
            return modelAndView;
        }

    }

    /**
     * 根据图书名称查询图书信息
     * 客户端查看图书详情
     * @param bookname
     * @return
     */
    @RequestMapping("/showParticulars/{bookname}")
    public ModelAndView showParticulars(@PathVariable("bookname") String bookname){
        System.out.println(bookname);
        System.out.println("根据图书名称查询图书信息。。");
        Book book = bookDao.showByBook(bookname);
        System.out.println("图书信息："+book);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("particulars");//跳转页面
        modelAndView.addObject("book",book);//携带数据
        return modelAndView;
    }

    /**
     * 添加购物车
     * @param shop
     * @return
     */
    @RequestMapping("/insertShop")
    public Map insertShop(Shop shop, HttpServletRequest request){
        System.out.println(shop);
        System.out.println("进入添加购物车。。");
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("user");
        //        获取session中的用户编号
        Integer uId = user.getUid();
        Shop shop1 = new Shop(null,uId,shop.getBookid(),shop.getBookcount(),shop.getPrice(),shop.getBookprice());
        int i1 = shopDao.insertShop(shop1);

        HashMap<Object, Object> map = new HashMap<>();
        if (i1 > 0) {
            map.put("code", "1");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "0");
            map.put("msg", "添加失败");
        }
        return map;



    }

}
